<?php

class NodeController extends Controller
{
	public function filters()
	{
		return array(
				'accessControl', // perform access control for CRUD operations
		);
	}
	
	/**
	 ** Specifies the access control rules.
	 ** This method is used by the 'accessControl' filter.
	 ** @return array access control rules
	 **/
	public function accessRules()
	{
		return array(
				array('allow',  // allow all users to perform 'index' and 'view' actions
						'actions'=>array('view', 'list'),
						'users'=>array('*'),
				),
				array('allow',  // allow all users to perform 'index' and 'view' actions
						'actions'=>array('add', 'delete'),
						'users'=>array('@'),
				),
				array("deny","users"=>array("*"))
		);
	}
	protected function beforeAction($action){
		if(!empty($_GET['username'])){
			/*
			 $member = Member::model()->find("username = :username",array("username"=>$_GET['username']));
			if(empty($member)){
			throw new CHttpException(404, "用户不存在");
			}elseif($member->type == Member::TYPE_GROUP){
			$this->layout = "//layouts/group2";
			}*/
		}else{
			throw new CHttpException(404, "用户不存在");
		}
		return true;
	}

	public function actionView($id)
	{
		$node = Node::model()->with("member")->findByPk($id);
		$this->pageTitle = $node->title . " - " . $node->member->title;
		$this->render('view',array('node'=>$node));
	}
	
	public function actionDelete($id){
		if(!Node::model()->exists("member_id = ".Yii::app()->user->id." and id=$id") &&Yii::app()->user->name!='admin'){
			$this->redirect(array("category/index"));
			CApplication::end();
		}
		$node = Node::model()->findByPk($id);
		$node->delete();
		$this->redirect(array("member/index",'username'=>Yii::app()->user->name));
	}
	public function actionAdd($id=null,$type=null,$parent_id = null)
	{
		if(!Utils::displayBtn("")){
			$this->redirect(array("site/index"));
		}
		if(!empty($_POST['Node'])){
			if (empty($_POST['Node']['id'])){
				$node = new Node();
				$node->member_id = Yii::app()->user->id;
				//Init album
				$album = new Album();
				$album->member_id = Yii::app()->user->id;
				$album->save();
					
				$node->album_id = $album->id;
			}else{
				$node = Node::model()->findByPk($_POST['Node']['id']);
			}
			$node->attributes = $_POST['Node'];
			if($node->save()){
				$this->redirect(array("node/view",'id'=>$node->id,'username'=>Yii::app()->user->name));
			}else{
				$this->render("add",array("node"=>$node));
				CApplication::end();
			}
		}
		if(empty($id)){
			$node = new Node();
			$node->type = empty($type)? Node::TYPE_NEWS : $type;
			$node->parent_id = empty($parent_id) ? 0: $parent_id;
		}else{
			$node = Node::model()->findByPk($id);
		}
		$this->render('add',array("node"=>$node));
	}

	public function actionList($username){
		$member = Member::model()->find("username=:username",array("username"=>$username));
		$this->pageTitle = "博客列表 - " . $member->title;
		$nodes = Node::model()->findAll("member_id = :member_id and type=:type",array("member_id"=>$member->id,"type"=>Node::TYPE_NEWS));
		$this->render("list",array("nodes"=>$nodes));
	}
}